
#ifndef ___WARD__PLANETNAMER_CPP
#define ___WARD__PLANETNAMER_CPP
#include <fstream>

#include "planetNamer.h"

planetNamer* planetNamer::pInstance = 0;

planetNamer* planetNamer::Instance()
{

if(pInstance == 0)
	pInstance = new planetNamer();

return pInstance;
}



planetNamer::planetNamer()
{
pInstance = 0;
first = getNames("first.pnm");
mid = getNames("mid.pnm");
last = getNames("last.pnm");
srand(time(NULL));

}

vector<string> planetNamer::getNames(string file)
{
vector <string> ret;
string r;
ifstream ifs(file.c_str());

while(!ifs.eof())
{
	getline(ifs,r);
	ret.push_back(r);
}
return ret;
}

	//random first, mid, last
string planetNamer::genName()
{
if(rand()%100 < 5)
return genName("");

return genName(first[rand()%first.size()]);
}

	//first is supplied; random mid, last
string planetNamer::genName(string f)
{
if(rand()%100 < 10)
	return genName(f,"");

return genName(f,mid[rand()%mid.size()]);
}

	//first, mid are supplied; random last
string planetNamer::genName(string f, string m)
{
string l;

if(((f!="")||(m!=""))&&(rand()%100<20)) if(f!="") return f + " " + m; else return m;
do
{
l = last[rand()%last.size()];
}
while((f=="")&&(m=="")&&(l=="-n"));


if(f != "")			//Add spacing for non-null strings
	if((m != "")||(l != "-n"))	//add space if mid is not null OR last is not n-suffix
		f+= " ";
if(m != "")			//Add space for null middle
	if(l != "-n")	//except for when last is a number suffix
	m+= " ";

if(l == "-n")
{
	char* buffer = new char[3];
	_itoa_s(rand()%1000,buffer, sizeof(int), 10);
	l = "-" + string(buffer);
}


return f + m + l;

}



#endif